package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.database.MyDataBase;
import com.lanou.dllo.findfood.entity.Dardry;
import com.lanou.dllo.findfood.tools.ToastCommon;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by dllo on 15/10/26.
 */
public class DiarySecondAtivity extends Activity{
    EditText edMenu,edItme;
    Button saveBtn,shearbtn;
    MyDataBase myDatabase;
    Dardry cun;
    int ids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        edMenu=(EditText) findViewById(R.id.editMenuName);
        edItme=(EditText) findViewById(R.id.editItem);
        saveBtn=(Button) findViewById(R.id.saveBtn);
        myDatabase=new MyDataBase(this);
        shearbtn = (Button) findViewById(R.id.sharebtn);
        shearbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //分享菜谱的功能 使用Intent.ACTION_SEND分享
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "我给你分享一道我自己的菜谱 菜名:："+edMenu.getText().toString()+"    步骤:"+edItme.getText().toString());
                startActivity(intent);
            }
        });

        Intent intent=this.getIntent();
        ids=intent.getIntExtra("ids", 0);

        if(ids!=0){
            cun=myDatabase.getTiandCon(ids);
            edMenu.setText(cun.getTitle());
            edItme.setText(cun.getContent());
        }

        saveBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                isSave();
            }
        });
    }



    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        //super.onBackPressed();
        SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy.MM.dd  HH:mm:ss");
        Date   curDate   =   new   Date(System.currentTimeMillis());
        String times   =   formatter.format(curDate);
        String title=edMenu.getText().toString();
        String content=edItme.getText().toString();

        if(ids!=0){
            cun=new Dardry(title,ids, content, times);
            myDatabase.toUpdate(cun);//保存我数据到数据库
            Intent intent=new Intent(DiarySecondAtivity.this,DiaryFristActvity.class);
            startActivity(intent);
            DiarySecondAtivity.this.finish();
        }

        else{
            if(title.equals("")&&content.equals("")){
                Intent intent=new Intent(DiarySecondAtivity.this,DiaryFristActvity.class);
                startActivity(intent);
                DiarySecondAtivity.this.finish();
            }
            else{
                cun=new Dardry(title,content,times);
                myDatabase.toInsert(cun);
                Intent intent=new Intent(DiarySecondAtivity.this,DiaryFristActvity.class);
                startActivity(intent);
                DiarySecondAtivity.this.finish();
            }

        }
    }

    private void isSave(){
        SimpleDateFormat formatter   =   new   SimpleDateFormat   ("yyyy.MM.dd  HH:mm:ss");
        Date curDate   =   new   Date(System.currentTimeMillis());
        String times   =   formatter.format(curDate);

        String title=edMenu.getText().toString();
        String content=edItme.getText().toString();
        if (title.length()==0){
            ToastCommon.createToastConfig().ToastShow(this,
                    (ViewGroup) findViewById(R.id.toast_layout_root), "菜名不能为空");
        }
        else if (content.length()==0){
            ToastCommon.createToastConfig().ToastShow(this,
                    (ViewGroup) findViewById(R.id.toast_layout_root),"步骤是空的( ⊙ o ⊙ )！请添加步骤");
        }
        else if(ids!=0){
            cun=new Dardry(title,ids, content, times);
            myDatabase.toUpdate(cun);//更新日记表
            Intent intent=new Intent(DiarySecondAtivity.this,DiaryFristActvity.class);
            startActivity(intent);
            DiarySecondAtivity.this.finish();
        }

        else{
            cun=new Dardry(title,content,times);
            myDatabase.toInsert(cun);//保存到日记本中 插入数据
            Intent intent=new Intent(DiarySecondAtivity.this,DiaryFristActvity.class);
            startActivity(intent);
            DiarySecondAtivity.this.finish();
        }
    }


}
